﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using System.Drawing;
using Tao.OpenGl;

namespace Clandestine
{
    public class OpenTkManager : IGlManager
    {
        public GameWindow GameWindow;

        public string Name
        {
            get { return "OpenTk"; }
        }

        public void Initialise(int zoomOverride, int ScreenWidth, int ScreenHeight)
        {
            // This will be the first call to OpenTK, so here we'll find out if the assembly failed to load!
            // should probably wrap the exception and make it neat or something, but idk right now.
            bool onWindows = Configuration.RunningOnWindows;

            int desktopWidth = DisplayDevice.Default.Width;
            int desktopHeight = DisplayDevice.Default.Height;

            int zoomLevel = 1;

            if (zoomOverride == 0)
            {
                if (desktopWidth < 1280 || desktopHeight < 800)
                {
                    /* MessageBox.Show("Your screen resolution is only " + vidDesktop.Width.ToString()
                         + "x" + vidDesktop.Height.ToString() + "! The window will be displayed at 1x zoom level, whereas "
                         + "the beautiful and glorious game designers envisioned it for 2x.\n\nAh well. Who cares, right?",
                         "Small Screen", MessageBoxButtons.OK, MessageBoxIcon.Warning);*/
                }

                while ((ScreenWidth * (zoomLevel * 2)) <= desktopWidth
                    && (ScreenHeight * (zoomLevel * 2)) <= desktopHeight)
                {
                    zoomLevel *= 2;
                }
            }
            else
            {
                zoomLevel = zoomOverride;
            }

            GameWindow = new GameWindow(ScreenWidth * zoomLevel, ScreenHeight * zoomLevel,
                    OpenTK.Graphics.GraphicsMode.Default, "", GameWindowFlags.Default);

            Fonts.SetPrecisionScale(zoomLevel);

            GameWindow.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(gw_Closing);
        }

        void gw_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // ints? reaaaally?
            e.Cancel = (OnWindowClose() == 1);
        }

        public void Deinitialise()
        {
            GameWindow.Close();
            GameWindow.Dispose();
        }

        public void OnRender()
        {
            DateTime dtStart = DateTime.Now;
            GameWindow.SwapBuffers();
            DateTime dtEnd = DateTime.Now;

            double ms = (dtEnd - dtStart).TotalMilliseconds;

            if (ms > 20)
            {
                string[] words = new string[] { "CUNT", "SHIT", "FUCK", "HNNNNNNG" };
                Random r = new Random();
                string f = words[r.Next(4)] + (r.Next(11) > 5 ? "" : words[r.Next(4)]) + "!";
                Log.w(f + " (" + ms.ToString() + "ms to SwapBuffers())");
            }
            //GameWindow.ProcessEvents();
        }

        public void GetViewportSize(out int w, out int h)
        {
            Size rect = GameWindow.ClientSize;
            w = rect.Width;
            h = rect.Height;
        }

        private int OnWindowClose()
        {
            lock (Graphics.RenderLockBlob)
                Program.Running = false;
            return 1; // Don't close (yet)!
        }

        public void SetWindowTitle(string s)
        {
            GameWindow.Title = s;
        }
    }
}
